module.exports = function ($scope, transData, $resource) {

	$scope.maxSize = '5';			//最多显示多少个按钮
    $scope.currentPage = '1';		//当前页码
    $scope.itemsPerPage = '20'		//每页显示几条

	$scope.what = 'list';		//查看状态 modify:修改用户,list:用户列表 add:添加用户
	$scope.obj = {};			//用户对象
	$scope.company_name = '';
	$scope.status = {			//userlist adduser modifyuser控制标签页的活跃状态
		'userlist' : true,
		'adduser' : false,
		'modify': false,
		'flag': true,
	};

	$scope.predicate = '';
	$scope.comparator = false;

	//初始化用户树结构
	$resource('/api/ac/sc/office/getList', {}, {}).save({}, function (res) {
		if (res.errcode !== 0) {
			alert(res.errmsg);
			return;
		};
		$scope.dataForTheTree = transData(res.data, 'id', 'parent_id', 'children');
		$scope.loadrolelist();
	});

	//加载角色列表-修改用户新建用户时选择权限使用
	$scope.loadrolelist = function () {
		$resource('/api/as/sc/role/getForUserList', {}, {}).save({}, function (res) {
			if (res.errcode === 0) {
				$scope.role_list = res.data;
			} else {
				alert(res.errmsg);
			}
		});
	};

	//菜单树选中事件
	$scope.showSelected = function (item) {
		$scope.what = 'list';
		$scope.status.userlist = true;
		$scope.status.flag = false;
		$scope.company_name = item.name;
		$scope.company_id = item.id;
		$scope.search.loginname = '';
		$scope.search.username = '';
		var para = {
			'pageNo': '1',
			'pageSize': $scope.itemsPerPage,
			'id': item.id
		}
		$resource('/api/ac/sc/systemUserService/getUserList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope.userList = res.data.results;
				$scope.totalItems = res.data.totalRecord;
			} else {
				alert(res.errmsg);
			}
		});
	};

	//搜索
	$scope.search = function (pageNo) {
		$scope.what = 'list';
		var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
			'loginname': $scope.search.loginname,
			'username': $scope.search.username
		};
		if($scope.company_id == undefined){
			para.id = $scope.dataForTheTree[0].id;
		} else {
			para.id = $scope.company_id;
		}
		$resource('/api/ac/sc/systemUserService/getUserList', {}, {}).save(para, function (res) {
			if (res.errcode === 0) {
				$scope.userList = res.data.results;
				$scope.totalItems = res.data.totalRecord;
			} else {
				alert(res.errmsg);
			}
		});
	};

	//点击页数查询
	$scope.change = function(pageNo){
		$scope.search(pageNo);
	}

	//添加界面
	$scope.add = function () {
		$scope.obj = {};
		$scope.obj.login_flag = '1';
		for(var i = 0; i < $scope.role_list.length; i++){
			$scope.role_list[i].active = false;
		}
	};

	//添加用户
	$scope.addUser = function () {
		$scope.obj.company_id = $scope.company_id;
		$scope.obj.rolelist = [];
		for(var i = 0; i < $scope.role_list.length; i++){
			if($scope.role_list[i].active){
				$scope.obj.rolelist.push($scope.role_list[i]);
			}
		}
		$resource('/api/ac/sc/systemUserService/addUser', {}, {}).save($scope.obj, function (res) {
			if (res.errcode === 0) {
				var item = {'id': $scope.company_id, 'name': $scope.company_name};
				$scope.showSelected(item);
			} else {
				alert(res.errmsg);
			}
		});
	};

	//修改界面
	$scope.update = function (item) {
		$scope.what = 'modify';
		$scope.status.modify = true;
		if(item.flag == 1){
			$resource('/api/ac/sc/systemUserService/getById', {}, {}).save(item, function (res) {
				if (res.errcode === 0) {
					$scope.obj = res.data;
					$scope.obj.flag = 1;	//拦截无权限用户使用
					$scope.obj.color = item.color == 'true'?false:true;
					if(!$scope.obj.user_type){
						$scope.obj.user_type = '3';
					}
					for(var i = 0; i < $scope.role_list.length; i++){
						$scope.role_list[i].active = false;
						for(var j = 0; j < $scope.obj.rolelist.length; j++){
							if($scope.obj.rolelist[j].id == $scope.role_list[i].id){
								$scope.role_list[i].active = true;
							}
						}
					}
				} else {
					alert(res.errmsg);
				}
			});
		}
	};

	//删除用户
	$scope.delete = function (item) {
		if (confirm('确认要删除该用户吗？')==true){
			item.del = '1';
			$resource('/api/ac/sc/systemUserService/setUserInfo', {}, {}).save(item, function (res) {
				if (res.errcode === 0) {
					item.id = item.company_id;
					$scope.showSelected(item);
				} else {
					alert(res.errmsg);
				}
			});
		}
	};

	//保存用户
	$scope.save = function () {
		$scope.obj.rolelist = [];
		for(var i = 0; i < $scope.role_list.length; i++){
			if($scope.role_list[i].active){
				$scope.obj.rolelist.push($scope.role_list[i]);
			}
		}
		if($scope.obj.flag == 1){
			$resource('/api/ac/sc/systemUserService/setUserInfo', {}, {}).save($scope.obj, function (res) {
				if (res.errcode === 0) {
					$scope.obj.id = $scope.obj.company_id;
					$scope.obj.name = $scope.obj.company_name;
					$scope.cancel();
				} else {
					alert(res.errmsg);
				}
			});
		}
	};

	//取消
	$scope.cancel = function () {
		$scope.what = 'list';
		$scope.status.userlist = true;
	};

	//用户列表标签
	$scope.watch = function () {
		$scope.what = 'list';
	};


};